package com.junjiao.util.java;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;

public class XmlBuildUtil {

	public static final String ENCODING = "UTF-8";

	/**
	 * 输出XML文档
	 * 
	 * @param out
	 *            输出流
	 * @param xmlDoc
	 *            XML文档
	 * @param encoding
	 *            输出编码格式
	 * @throws IOException
	 *             IO异常
	 */
	public static void writeXMlResponse(OutputStream out, Document xmlDoc,
			String encoding) throws IOException {

		OutputFormat formatter = new OutputFormat();
		formatter.setEncoding(encoding);

		XMLWriter xmlWriter = null;
		try {
			xmlWriter = new XMLWriter(out, formatter);
			xmlWriter.write(xmlDoc);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 生成空头部
	 * 
	 * @param doc
	 * @param listPage
	 * @param userID
	 * @param userName
	 * @return
	 */
	public static Element buildNullRoot(Document doc, String userID,
			String userName) {
		doc.setXMLEncoding(ENCODING);
		Element root = doc.addElement("objectSet");
		root.addAttribute("userid", userID);
		root.addAttribute("username", userName);
		return root;
	}

	/**
	 * "标题标题"--转化后---"标题..."
	 * 
	 * @param str
	 * @param length
	 * @return
	 */
	public static String regular(String str, int length) {
		if (length == 0) {
			return str;
		}
		String temp = str;
		if (temp == null) {
			return "";
		}
		if (temp.length() > length + 1) {
			temp = temp.substring(0, length);
		}
		return temp;
	}

	/**
	 * "从冒号为起始,开始截长"
	 * 
	 * @param str
	 * @param length
	 * @return
	 */
	public static String regularBegin(String str, int blength, int length) {
		if (length == 0) {
			return str;
		}
		if (blength == 0) {
			return "";
		}
		String temp = str;
		if (temp == null) {
			return "";
		}
		if (temp.length() > length + 1) {
			temp = temp.substring(blength + 1, length);
		}
		return temp;
	}

	/**
	 * "标题标题"--转化后---"标题..."
	 * 
	 * @param str
	 * @param length
	 * @return
	 */
	public static String regularText(String str, int length) {
		if (length == 0) {
			return str;
		}
		String temp = str;
		if (temp == null) {
			return "";
		}
		if (temp.length() > length + 1) {
			temp = temp.substring(0, length) + "...";
		}
		return temp;
	}

	/**
	 * 显示中文的时间差 如一个小时前
	 * 
	 * @param distance
	 * @return
	 */
	public static String dateToString(Long distance) {
		String result = "";
		long date = distance / 60000;

		result = date % 60 + "分钟";
		date /= 60;
		if (date > 0) {
			result = date % 24 + "小时" + result;
			date /= 24;
		} else if (date > 0) {
			result = date + "天" + result;
		}
		result += "前";
		return result;
	}

	public static <T> T xml2Bean(Class<T> T, File file) {
		return null;
	}
}
